using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SystemFunctionLib.Multimedia;
using SystemFunctionLib.Apis;

namespace SystemFunctionDemo
{
    public partial class AudioMixerForm : Form
    {
        ComponentType[] cmpTypes = null;
        TrackBar[] trackBars = null;
        CheckBox[] checkBoxes = null;

        public AudioMixerForm()
        {
            InitializeComponent();

            this.Font = SystemFonts.DialogFont;

            cmpTypes = new ComponentType[]
            {
                ComponentType.DestSpeakers,
                ComponentType.SrcWaveOut,
                ComponentType.SrcSWSynth,
                ComponentType.SrcCDPlayer,
                ComponentType.SrcPCSpeaker
            };

            trackBars = new TrackBar[]
            {
                trackVolume,
                trackWave,
                trackSWSynth,
                trackCDPlayer,
                trackPCSpeaker
            };

            checkBoxes = new CheckBox[]
            {
                chkOverallMuted,
                chkWaveMuted,
                chkSWSynthMuted,
                chkCDPlayerMuted,
                chkPCSpeakerMuted
            };

            LoadData();
        }

        private void LoadData()
        {
            double? vol = 0;
            bool? muted = null;

            for (int i = 0; i < cmpTypes.Length; i++)
            {
                vol = AudioMixer.GetVolume(cmpTypes[i]);
                if (vol != null)
                {
                    trackBars[i].Value = (int)(vol * 100);
                }
                else
                {
                    trackBars[i].Value = 0;
                    trackBars[i].Enabled = false;
                }

                muted = AudioMixer.GetIsMuted(cmpTypes[i]);
                if (muted != null)
                {
                    checkBoxes[i].Checked = muted.Value;
                }
                else
                {
                    checkBoxes[i].Checked = false;
                    checkBoxes[i].Enabled = false;
                }
            }
        }

        private void SaveData()
        {
            for (int i = 0; i < cmpTypes.Length; i++)
            {
                AudioMixer.SetVolume(cmpTypes[i], trackBars[i].Value / 100.0);
                AudioMixer.SetIsMuted(cmpTypes[i], checkBoxes[i].Checked);
            }
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            SaveData();
        }
    }
}